package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.JoinTeam;
import com.ly.entity.Member;
import com.ly.mapper.JoinTeamMapper;
import com.ly.mapper.MemberMapper;
import com.ly.mapper.TeamMapper;
import com.ly.pojo.dto.JoinTeamDTO;
import com.ly.pojo.vo.JoinTeamVO;
import com.ly.service.JoinTeamService;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author : 黑渊白花
 * @ClassName JoinTeamServiceImpl
 * @date : 2025/2/8 22:32
 * @Description
 */
@Service
@Slf4j
public class JoinTeamServiceImpl extends ServiceImpl<JoinTeamMapper, JoinTeam> implements JoinTeamService {
    @Autowired
    private JoinTeamMapper joinTeamMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private TeamMapper teamMapper;

    @Override
    public IPage<JoinTeamVO> getAllPage(JoinTeamDTO joinTeamDTO) {
        Integer pageNum = joinTeamDTO.getPageNum();
        Integer pageSize = joinTeamDTO.getPageSize();
        String stuName = joinTeamDTO.getUserName();
        String teamName = joinTeamDTO.getTeamName();
        IPage<JoinTeamVO> page = new Page<>(pageNum,pageSize);
        IPage<JoinTeamVO> allPage = joinTeamMapper.getAllPage(page,stuName,teamName);
        return allPage;
    }

    @Override
    public IPage<JoinTeamVO> getPage(JoinTeamDTO joinTeamDTO) {
        Integer pageNum = joinTeamDTO.getPageNum();
        Integer pageSize = joinTeamDTO.getPageSize();
        String stuName = joinTeamDTO.getUserName();
        Integer teamId = joinTeamDTO.getTeamId();
        Integer status = joinTeamDTO.getStatus();
        IPage<JoinTeamVO> page = new Page<>(pageNum,pageSize);
        IPage<JoinTeamVO> stPage = joinTeamMapper.getPage(page,teamId,stuName,status);
        return stPage;
    }

    //修改表的状态、添加状态表，同时社团成员数+1
    @Override
    public void auditJoinTeam(JoinTeam joinTeam) {
        joinTeamMapper.updateById(joinTeam);
        Date d = new Date();
        Member member = new Member();
        member.setStuId(joinTeam.getStuId());
        member.setCreateTime(d);
        member.setTeamId(joinTeam.getTeamId());
        memberMapper.insert(member);
        //社团成员+1
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id",joinTeam.getTeamId());
        wrapper.setSql("number = number + 1");
        teamMapper.update(wrapper);
    }

    @Override
    public Long getJoinTNumByTeamId(Integer teamId) {
        QueryWrapper<JoinTeam> qw = new QueryWrapper<JoinTeam>();
        qw.eq("team_id", teamId);
        Long count = joinTeamMapper.selectCount(qw);
        return count;
    }

    @Override
    public Result applyTeam(JoinTeam joinTeam) {
        //先判断是否是成员
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id",joinTeam.getStuId());
        queryWrapper.eq("team_id",joinTeam.getTeamId());
        Long count = memberMapper.selectCount(queryWrapper);
        if(count==1){
            return Result.warn("用户已经是该社团的成员了") ;
        }
        //不是成员，再判断是否是否已经发出请求
        QueryWrapper<JoinTeam> queryJT = new QueryWrapper<>();
        queryJT.eq("stu_id",joinTeam.getStuId());
        queryJT.eq("team_id",joinTeam.getTeamId());
        queryJT.eq("status",0);
        //判断学生是否已经报名
        Long count1 = joinTeamMapper.selectCount(queryJT);
        if(count1==1){
            return Result.warn("用户已经发出请求");
        }
        joinTeam.setStatus(0);
        joinTeamMapper.insert(joinTeam);
        return Result.success("申请成功","申请成功");
    }

    @Override
    public Boolean isapplyTeam(JoinTeam joinTeam) {
        QueryWrapper<JoinTeam> queryJT = new QueryWrapper<>();
        queryJT.eq("stu_id",joinTeam.getStuId());
        queryJT.eq("team_id",joinTeam.getTeamId());
        queryJT.eq("status",0);
        //判断学生是否已经报名
        Long count1 = joinTeamMapper.selectCount(queryJT);
        if(count1==0){
            return false;
        }
        return true;
    }
}
